/**
 * inforcreation.com co.Ltd
 * MichaelHuang
 * 2011-12-16
 */
package com.zchat.android.receivers;

import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.zchat.api.ZChat;
import com.zchat.api.cache.Cache;
import com.zchat.api.connection.ReadStatus;
import com.zchat.api.connection.ReadStatusUtil;
import com.zchat.api.log.Logger;
import com.zchat.api.util.ZChatSetting;

/**
 * 
 * Receive the read status broadcaster.
 * @author Oliverzhu
 *	2012/6/14
 */
public class ReadStatusReceiver extends BroadcastReceiver {
	public static final String READ_STATUS_CHANGED = "read_status_changed";
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if(!action.equals(READ_STATUS_CHANGED)) {
			return ;
		}
		if(action.equals(READ_STATUS_CHANGED)) {
			//add status to cache
			Cache cacheManager = ZChat.getCache();
			if(cacheManager != null) {
				Object value =  cacheManager.get("read_status");
				if(value != null) {
					ReadStatus preStatus = ReadStatus.stringToEnum(value.toString());
					if(preStatus == ReadStatusUtil.getInstance().getStatus()) {
						return;
					}
				}
				cacheManager.put("read_status", ReadStatusUtil.getInstance().getStatus());
				
			}
			if(ZChatSetting.DEBUG) Logger.info("ReadStatusReceiver-接收到改变阅读状态广播，将改为：" + ReadStatusUtil.getInstance().getStatus());
		}
	}
	
	public void register(Application application)
	{
		IntentFilter readStatusFilter = new IntentFilter();
		application.registerReceiver(this, readStatusFilter);
	}

}
